package com.aliang.shopping.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aliang.shopping.common.PageResult;
import com.aliang.shopping.common.ResultCodeEnum;
import com.aliang.shopping.config.OssConfig;
import com.aliang.shopping.contact.BasicConstant;
import com.aliang.shopping.exception.DataException;
import com.aliang.shopping.exception.ServiceException;
import com.aliang.shopping.mapper.NoticeMapper;
import com.aliang.shopping.mapper.UserInfoMapper;
import com.aliang.shopping.model.converter.CommonConverter;
import com.aliang.shopping.model.converter.NoticeConverter;
import com.aliang.shopping.model.dto.common.PageDTO;
import com.aliang.shopping.model.enums.UserTypeEnum;
import com.aliang.shopping.model.po.Notice;
import com.aliang.shopping.model.vo.chat.SimpleContactVO;
import com.aliang.shopping.model.vo.notice.NoticeExportVO;
import com.aliang.shopping.model.vo.notice.NoticeVO;
import com.aliang.shopping.security.auth.Payload;
import com.aliang.shopping.security.auth.UserHolder;
import com.aliang.shopping.service.NoticeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author ShiBaLover
 * @description 针对表【notice】的数据库操作Service实现
 * @createDate 2024-10-05 17:12:17
 */
@Service
@AllArgsConstructor
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
        implements NoticeService {
    private final CommonConverter<NoticeVO> commonConverter = new CommonConverter<>();


    private final NoticeConverter noticeConverter;
    private final OssConfig ossConfig;
    private final UserInfoMapper userInfoMapper;

    @Override
    public PageResult<NoticeVO> selectPage(PageDTO pageDTO) {
        // 获取当前登录角色
        Payload payload = UserHolder.get();
        if (ObjectUtil.isEmpty(payload)) {
            // 没有登录，返回错误
            throw new ServiceException(ResultCodeEnum.USER_NOT_LOGIN);
        }


        //查询数据
        IPage<NoticeVO> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        //如果是商户，只能查询自己的数据
        IPage<NoticeVO> mapperPage;
        if (payload.getRole() <= UserTypeEnum.BUSINESS.getCode()) {
            mapperPage = this.baseMapper.getPage(page, pageDTO, payload.getId());
        } else {
            mapperPage = this.baseMapper.getPage(page, pageDTO, null);
        }
        //获取创建人Id
        if (!mapperPage.getRecords().isEmpty()) {
            Set<Long> collect = mapperPage.getRecords().stream().map(NoticeVO::getUserId).collect(Collectors.toSet());
            List<SimpleContactVO> nameById = this.userInfoMapper.getNameById(collect);
            //转换成map
            Map<Long, String> nameMap = nameById.stream().collect(Collectors.toMap(SimpleContactVO::getId, SimpleContactVO::getNickname));
            //封装数据
            mapperPage.getRecords().forEach(vo -> {
                // 设置完整路径,替代符号转换成完整路径
                vo.setContent(StrUtil.replace(vo.getContent(), BasicConstant.REPLACE_IMAGE, ossConfig.getUrl()));
                // 设置名称
                vo.setName(nameMap.get(vo.getUserId()));
            });
        }
        //转换数据
        return commonConverter.toPageResult(mapperPage);
    }

    @Override
    public List<NoticeExportVO> getExportList(PageDTO pageDTO) {
        List<NoticeVO> list = this.baseMapper.getVoList(pageDTO,UserHolder.getUserId());
        if (list.isEmpty()) {
            throw new DataException(ResultCodeEnum.NOT_QUERY_RESULT);
        }
        //转换数据
        return noticeConverter.toImportDTOList(list);
    }
}




